package cn.itsy.web.controller;

import cn.itsy.pojo.Warnrule;
import cn.itsy.pojo.Warntype;
import cn.itsy.service.warntype.WarnTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:
 * @author: MisterGay
 * @date: 2019-06-03 11:21
 */
@Controller
@RequestMapping(value = "warnType")
public class WarnTypeController {
    @Resource(name = "warntypeimpl")
    private WarnTypeService warnTypeService;

    /**
     * 查询所有预警类型
     *
     * @return
     */
    @RequestMapping(value = "queryWarnTypeAll", method = RequestMethod.GET)
    @ResponseBody
    public List<Warntype> queryWarnTypeAll() {
        String sql = "select * from warntype where status='Y' ";
        List<Warntype> warntypes = this.warnTypeService.queryWarntypeAll(sql);
        return warntypes;
    }

    /**
     * 添加预警类型
     */
    @RequestMapping(value = "insertWarnType", method = RequestMethod.POST)
    @ResponseBody
    public void insertWarnType(Warntype warntype) {
        String sql = "insert into warntype values(?,?,?,?,?)";
        this.warnTypeService.insertWarnType(sql, warntype);
    }

    /**
     * 修改预警类型
     */
    @RequestMapping(value = "updateWarnType", method = RequestMethod.POST)
    @ResponseBody
    public void updateWarnType(Warntype warntype) {
        System.out.println(warntype);
        String sql = "update warntype set id=?,wtno=?,wtname=?,wtdesc=?,status=? where id=?";
        this.warnTypeService.updateWarnType(sql, warntype);
    }

    /**
     * 删除预警类型
     */
    @RequestMapping(value = "deletewarnTypeById", method = RequestMethod.GET)
    @ResponseBody
    public Integer deletewarnTypeById(Warntype warntype) {
        System.out.println(warntype);
        String querySql = "select * from warnrule where warntype_id=?";
        List<Warnrule> warnrules = this.warnTypeService.queryWarnruleById(querySql, warntype);
        if (warnrules.isEmpty()) {
            /**
             * 逻辑删除预警类型(修改其状态)
             */
            String updateSql = "update warntype set status='N' where id=? ";
            this.warnTypeService.updateWarnTypeStatusById(updateSql, warntype);
            return 1;
        }
        return 0;
    }
}
